import { Component, OnInit } from '@angular/core';
import { ApplyQDIINameService } from './apply-qdiiname.service';
import * as _ from "lodash";
import { DataService } from '../../../../../services/data.service';

@Component({
  selector: 'app-apply-qdiiname',
  templateUrl: './apply-qdiiname.component.html',
  styleUrls: ['./apply-qdiiname.component.scss'],
  providers: [ApplyQDIINameService]
})
export class ApplyQDIINameComponent implements OnInit {
  //发送过来的数据下标
  public index: any;
  //判断查询按钮是否已点击
  public searchIsChecked: any = false;
  //从上一层获取到的默认勾选的数据id
  public checkedIdList: any = [];
  //模式类型
  public modeType: any;//input表示录入模式，route表示在途模式
  //根据类型分类的list
  public fundTypeList: any;
  public fundTypeInfo: any;
  //根据搜索条件得到的list
  public searchList: any = [];
  //基金组list
  public groupList: any = [];
  //账户名称
  public sFundName: any;
  //产品分类以及搜索全选按钮
  public typeCheckedAll: any = false;
  //基金组全选按钮
  public groupCheckedAll: any = false;
  //保存时已选择的用户和基金组
  constructor(
    public applyQDIINameService: ApplyQDIINameService,
    public dataService: DataService
  ) { }

  ngOnInit() {
    //将接受到的数据转为number类型
    _.forEach(this.checkedIdList, (item, index) => {
      this.checkedIdList[index] = parseInt(this.checkedIdList[index]);
    })
    this.listByFundType();
    this.fundGroupList();
  }

  // 列表全选
  checkedAll(data, checkedAll) {

    if (checkedAll) {
      // 更新为全选

      _.forEach(data, item => {
        item.isChecked = true;
      });
    } else {
      // 更新为不全选
      _.forEach(data, item => {
        item.isChecked = false;
      });
    }
  }
  // 列表是否全选
  ctrlCheckAll(data, checkAllBtn) {
    let that = this;
    let checkList = [];
    _.forEach(data, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == data.length) {
      this.checkedAllType(checkAllBtn, true);
    } else {
      this.checkedAllType(checkAllBtn, false);
    }
  }
  //判断全选按钮类型
  checkedAllType(checkAllBtn, isTrue) {
    switch (checkAllBtn) {
      //产品类型全选按钮
      case 'typeCheckedAll':
        this.typeCheckedAll = isTrue;
        break;
      //基金组全选按钮
      case 'groupCheckedAll':
        this.groupCheckedAll = isTrue;
        break;
    }
  }
  //适用名称搜索弹出框产品类型tab
  listByFundType() {
    let that = this;
    let getBody: any = {
      sType: 1
    }
    this.applyQDIINameService.listByFundType(getBody).subscribe(data => {
      if (data) {
        this.fundTypeInfo = data;
        if (this.checkedIdList.length > 0) {
          _.forIn(that.fundTypeInfo, (value, key) => {
            _.forEach(value, item => {
              if (_.includes(that.checkedIdList, item.nFundNo)) {
                item.isChecked = true;
              }
            })
          })
        }
        this.selectType('公募QDII');
        console.log(data);
      }
    })
  }
  //点击tab根据产品类型将数据添加到list中
  selectType(type) {
    let that = this;
    _.forIn(this.fundTypeInfo, (value, key) => {
      if (type == key) {
        that.fundTypeList = value;
        that.ctrlCheckAll(that.fundTypeList, 'typeCheckedAll');
      }
    })
  }
  //获取适用QDII名称列表列表
  fundGroupList() {
    let that = this;
    let getBody = {
      sType: 1  //1代表QDII，0代表非QDII
    }
    this.applyQDIINameService.fundGroupList(getBody).subscribe(data => {
      if (data) {
        this.groupList = data;
        if (this.checkedIdList.length > 0) {
          _.forEach(this.groupList, item => {
            if (_.includes(that.checkedIdList, item.nFgId)) {
              item.isChecked = true;
            }
          })
        }
      }
    })
  }
  //适用名称搜索弹出框搜索
  searchlist(item?) {
    let that = this;
    let getBody = {
      sFundName: this.sFundName,
      sType: 1
    }
    if (item == 'search') {
      this.searchIsChecked = true;
    }
    this.applyQDIINameService.searchList(getBody).subscribe(data => {
      if (data) {
        this.searchList = data;
      }
    })
  }
  //保存选择的用户
  saveSelect() {
    let that = this;
    let saveList: any = [];
    let saveListItem: any;
    let sFundNoList: any = [];
    let sFundNoItem: any;
    _.forIn(this.fundTypeInfo, (value, key) => {
      _.forEach(value, item => {
        if (item.isChecked) {
          saveList.push(item.sFundName);
          sFundNoList.push(item.nFundNo);
        }
      })
    })
    _.forEach(this.searchList, item => {
      if (item.isChecked) {
        saveList.push(item.sFundName);
        sFundNoList.push(item.nFundNo);
      }
    })
    if (this.modeType != 'searchFundCode') {
      _.forEach(that.groupList, item => {
        if (item.isChecked) {
          saveList.push(item.sFgName);
          sFundNoList.push(item.nFgId);
        }
      })
    }

    saveList = _.uniq(saveList);
    saveListItem = saveList.join(',');
    that.dataService.QDIINameList = saveListItem;
    sFundNoList = _.uniq(sFundNoList);
    sFundNoItem = sFundNoList.join(',');
    that.dataService.QDIIIdList = sFundNoItem;
    if (this.modeType == 'input') { //录入模式适用QDII名称
      that.dataService.QDIIInputModelEmitter.emit('inputModel');
    } else if (this.modeType == 'route') {//在途模式适用QDII名称
      that.dataService.QDIIInputModelEmitter.emit('routeModel');
    } else if (this.modeType == 'searchFundCode') {
      that.dataService.QDIIInputModelEmitter.emit('searchFundCode');
    } else if (this.index != null) {
      that.dataService.QDIIIndexId = this.index;
      console.log(that.dataService.QDIIIndexId)
      that.dataService.QDIIInputModelEmitter.emit('index');
    }
    console.log(saveListItem)
    console.log(sFundNoItem)
  }
  //关闭modal
  hideModal() {
    window['$']('#applyQDIIFundCode').modal('hide');
    window['$']('#applyQDIIName').modal('hide');
    window['$']('#applyQDIIName1').modal('hide');
    window['$']('#QDIIViewApplyQDIIName').modal('hide');
  }
}
